package org.example;

import org.example.entity.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

/**
 * <p> 测试对 POJO 编解码 </p>
 *
 * @author Chris
 * @since 2024/3/25 22:59
 */
public class TestUserInfo {
    /**
     * 使用 slf4j 日志框架
     */
    private final static Logger log = LoggerFactory.getLogger(TestUserInfo.class);

    public static void main(String[] args) throws IOException {
        final UserInfo info = new UserInfo();
        info.buildUserID(100).buildUserName("Welcome to Netty");

        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(info);
        os.flush();
        os.close();

        final byte[] b = bos.toByteArray();
        log.info("The jdk serializable length is : {}", b.length);
        bos.close();
        log.info("---------------------------------------------");

        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        log.info("The byte array serializable length is : {}", info.codeC(buffer).length);
    }

}